import matplotlib.pyplot as plt
import matplotlib.patches as mp
import numpy as np
import qqmbr.odebook as ob
import warnings
fontsize = 20
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=-10.1, xmax=10, ymin=-10.1, ymax=10,fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
fs = lambda x, y: np.array([13 * x + 6 * y, -15 * x - 5 * y]) / np.linalg.norm(np.array([13 * x + 6 * y, -15 * x - 5 * y]))
ob.mquiver(np.linspace(-10, 10, 40), np.linspace(-10, 10, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
start_points = [1, 3, 5, 7.5]
inits = [(x, 0) for x in start_points]
inits += [(-x, 0) for x in start_points]
ob.phaseportrait(lambda X: np.array([13 * X[0] + 6 * X[1], -15 * X[0] - 5 * X[1]]),
inits, t=(-3, 3), n=100, linewidth=3, color='LIGHTCORAL')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
fontsize = 20
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=-10.1, xmax=10, ymin=-10.1, ymax=10,fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
fs = lambda x, y: np.array([4 * x - 3 * y, 3 * x + 4 * y]) / np.linalg.norm(np.array([4 * x - 3 * y, 3 * x + 4 * y]))
ob.mquiver(np.linspace(-10, 10, 40), np.linspace(-10, 10, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
start_points = [1, 3, 5, 7.5]
inits = [(x, 0) for x in start_points]
inits += [(-x, 0) for x in start_points]
ob.phaseportrait(lambda X: np.array([4 * X[0] - 4 * X[1], 3 * X[0] + 4 * X[1]]),
inits, t=(-3, 3), n=100, linewidth=3, color='LIGHTCORAL')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
Пункт (а).
fontsize = 20
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=-10.1, xmax=10, ymin=-20.1, ymax=20, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
fs = lambda x, y: np.array([10 * x - 6 * y, 15 * x - 8 * y]) / np.linalg.norm(np.array([10 * x - 6 * y, 15 * x - 8 * y]))
ob.mquiver(np.linspace(-10, 10, 40), np.linspace(-20, 20, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
start_points = [0.1, 0.15, 0.21]
inits = [(x, 0.205) for x in start_points]
ob.phaseportrait(lambda X: np.array([10 * X[0] - 6 * X[1], 15 * X[0] - 8 * X[1]]),
inits, t=(0, 5- 0.02), n=1000, linewidth=3, color='LIGHTCORAL')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
Пункт (b).
fontsize = 20
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=-10.1, xmax=10, ymin=-10.1, ymax=10, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
fs = lambda x, y: np.array([-16 * x + 16 * y, -12 * x + 12 * y]) / np.linalg.norm(np.array([-16 * x + 16 * y, -12 * x + 12 * y]))
ob.mquiver(np.linspace(-10, 10, 40), np.linspace(-10, 10, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
start_points = [1, 2, 3, 4]
inits = [(x, 0) for x in start_points]
inits += [(-x, 0) for x in start_points]
ob.phaseportrait(lambda X: np.array([-16 * X[0] + 16 * X[1], -12 * X[0] + 12 * X[1]]),
inits, t=(-40, 40), n=1000, linewidth=3, color='LIGHTCORAL')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
Пункт (c).
fontsize = 20
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=-10.1, xmax=10, ymin=-10.1, ymax=10, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
fs = lambda x, y: np.array([8 * x - 6 * y, 4 * x - 2 * y]) / np.linalg.norm(np.array([8 * x - 6 * y, 4 * x - 2 * y]))
ob.mquiver(np.linspace(-10, 10, 40), np.linspace(-10, 10, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
start_points = [1, 3, 5, 7.5, 15]
inits = [(x, 0) for x in start_points]
inits += [(-x, 0) for x in start_points]
ob.phaseportrait(lambda X: np.array([8 * X[0] - 6 * X[1], 4 * X[0] - 2 * X[1]]),
inits, t=(-40, 40), n=1000, linewidth=3, color='LIGHTCORAL')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
Пункт (d).
fontsize = 20
figure = plt.figure(figsize=(10, 10))
x_min = -10
x_max = 10
y_min = -10
y_max = 10
ob.axes4x4(xmin=x_min - 0.1, xmax=x_max, ymin=y_min - 0.1, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
fs = lambda x, y: np.array([-8 * x + 18 * y, -3 * x + 7 * y]) / np.linalg.norm(np.array([-8 * x + 18 * y, -3 * x + 7 * y]))
ob.mquiver(np.linspace(x_min, x_max, 40), np.linspace(y_min, y_max, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
start_points = [1, 5, 10, 20]
inits = [(x, 0) for x in start_points]
inits += [(-x, 0) for x in start_points]
ob.phaseportrait(lambda X: np.array([-8 * X[0] + 18 * X[1], -3 * X[0] + 7 * X[1]]),
inits, t=(-10, 10), n=1000, linewidth=3, color='LIGHTCORAL')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
Пункт (e).
fontsize = 20
figure = plt.figure(figsize=(10, 10))
x_min = -10
x_max = 10
y_min = -10
y_max = 10
ob.axes4x4(xmin=x_min - 0.1, xmax=x_max, ymin=y_min - 0.1, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
fs = lambda x, y: np.array([-5 * x - 2 * y, -5 * y]) / np.linalg.norm(np.array([-5 * x - 2 * y, -5 * y]))
ob.mquiver(np.linspace(x_min, x_max, 40), np.linspace(y_min, y_max, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
start_points = [0.01, 0.8, 1.4, 2]
inits = [(x, -1) for x in start_points]
inits += [(x, 1) for x in start_points]
inits += [(-x, 1) for x in start_points]
inits += [(-x, -1) for x in start_points]
ob.phaseportrait(lambda X: np.array([-5 * X[0] - 2 * X[1], -5 * X[1]]),
inits, t=(-2, 2), n=100, linewidth=3, color='LIGHTCORAL')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
from matplotlib import animation
fontsize = 20
x_min = -10
x_max = 10
y_min = -10
y_max = 10
figure = plt.figure(figsize=(10, 10))
count = 2000
delta = 100
S_min = -20
S_max = 20
S = np.concatenate([np.linspace(S_min, S_max, count), np.array([45 / 6, 49 / 6]), np.linspace(45 / 6 - 1, 49 / 6 + 1, delta)])
S.sort()
start_points = [1, 5, 10, 20]
inits = [(x, 0) for x in start_points]
inits += [(-x, 0) for x in start_points]
def plot_phase_portrait(s):
plt.clf()
ob.axes4x4(xmin=x_min - 0.1, xmax=x_max, ymin=y_min - 0.1, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
fs = lambda x, y: np.array([5 * x - 6 * y, S[s] * x - 9 * y]) / np.linalg.norm(np.array([5 * x - 6 * y, S[s] * x - 9 * y]))
ob.mquiver(np.linspace(x_min, x_max, 40), np.linspace(y_min, y_max, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
ob.phaseportrait(lambda X: np.array([5 * X[0] - 6 * X[1], S[s] * X[0] - 9 * X[1]]),
inits, t=(-2, 2), n=100, linewidth=3, color='LIGHTCORAL')
plt.legend(["s = {:.3f}".format(S[s])], loc="upper left")
return plt.plot()
anime = animation.FuncAnimation(figure, func=plot_phase_portrait, frames=len(S), blit=True)
anime.save("hw-03-3-c-long.mp4", fps=25, extra_args=["-vcodec", "libx264"])
from matplotlib import animation
from matplotlib import pyplot as plt
fontsize = 20
x_min = -10
x_max = 10
y_min = -10
y_max = 10
figure = plt.figure(figsize=(10, 10))
count = 300
delta = 100
S_min = 5
S_max = 12
S = np.concatenate([np.linspace(S_min, S_max, count), np.array([45 / 6, 49 / 6]),
np.linspace(45 / 6 - 1, 49 / 6 + 1, delta)])
S.sort()
S = np.concatenate([S, S[::-1]])
start_points = [1, 5, 10, 20]
inits = [(x, 0) for x in start_points]
inits += [(-x, 0) for x in start_points]
def plot_phase_portrait(s):
clf()
ob.axes4x4(xmin=x_min - 0.1, xmax=x_max, ymin=y_min - 0.1, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
fs = lambda x, y: np.array([5 * x - 6 * y, S[s] * x - 9 * y]) / np.linalg.norm(np.array([5 * x - 6 * y, S[s] * x - 9 * y]))
ob.mquiver(np.linspace(x_min, x_max, 40), np.linspace(y_min, y_max, 40),
fs, color='Teal', headlength=5, scale=3, scale_units='x')
ob.phaseportrait(lambda X: np.array([5 * X[0] - 6 * X[1], S[s] * X[0] - 9 * X[1]]),
inits, t=(-2, 2), n=100, linewidth=3, color='LIGHTCORAL')
plt.legend(["s = {:.3f}".format(S[s])], loc="upper left")
return plot()
anime = animation.FuncAnimation(figure, func=plot_phase_portrait, frames=len(S), blit=True)
anime.save("hw-03-3-c-short-reversed.mp4", fps=25, extra_args=["-vcodec", "libx264"])
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
w = 1
Y, X = np.mgrid[-w:w:1000j, -w:w:1000j]
U = np.sin(X) + np.exp(Y) - 1
V = np.sin(X - Y)
fontsize = 20
x_min = -1
x_max = 1
y_min = -1
y_max = 1
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=x_min, xmax=x_max, ymin=y_min, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.4, 1.4], color='LIGHTCORAL', linewidth=3)
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
w = 5
Y, X = np.mgrid[-w:w:1000j, -w:w:1000j]
U = np.sin(X) + np.exp(Y) - 1
V = np.sin(X - Y)
fontsize = 20
x_min = -5
x_max = 5
y_min = -5
y_max = 5
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=x_min, xmax=x_max, ymin=y_min, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.4, 1.4], color='LIGHTCORAL', linewidth=3)
plt.tight_layout()
plt.show()
w = 1
Y, X = np.mgrid[-w:w:1000j, -w:w:1000j]
U = X + Y
V = X - Y
fontsize = 20
x_min = -1
x_max = 1
y_min = -1
y_max = 1
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=x_min, xmax=x_max, ymin=y_min, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.4, 1.4], color='LIGHTCORAL', linewidth=3)
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
w = 5
Y, X = np.mgrid[-w:w:1000j, -w:w:1000j]
U = X + Y
V = X - Y
fontsize = 20
x_min = -5
x_max = 5
y_min = -5
y_max = 5
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=x_min, xmax=x_max, ymin=y_min, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.4, 1.4], color='LIGHTCORAL', linewidth=3)
plt.tight_layout()
plt.show()
def getDistance(x, y):
return np.sqrt((x[0] - y[0]) ** 2 + (x[1] - y[1]) ** 2)
def dxdt(x, y):
return -(np.sin(x) + np.exp(y) - 1)
def dydt(x, y):
return -(np.sin(x - y))
def traverseToZero(point):
dt = 1 / 10000
step = 0
while point[0] > 0 and point[1] > 0:
point[0] += dxdt(point[0], point[1]) * dt
point[1] += dydt(point[0], point[1]) * dt
return point
def findSeparatrixPoint():
steps = 10000
left = 1.25
right = 1.75
minDistance = 5 + 2
result = left
step = 0
for mid in np.linspace(left, right, steps):
start = [5., mid]
finish = traverseToZero(start)
distance = getDistance(finish, [0, 0])
if distance < minDistance:
minDistance = distance
result = mid
if step % 250 == 0:
print("step {}:\nresult = {}\ndistance = {}".format(step, result, minDistance))
step += 1
return result
print(findSeparatrixPoint())
def traverseToZero2(point):
dt = 1 / 100000
step = 0
while point[0] > 0 and point[1] > 0:
point[0] += dxdt(point[0], point[1]) * dt
point[1] += dydt(point[0], point[1]) * dt
return point
def findSeparatrixPoint2():
steps = 1000
left = 1.622
right = 1.626
minDistance = 5 + 2
result = left
step = 0
for mid in np.linspace(left, right, steps):
start = [5., mid]
finish = traverseToZero2(start)
distance = getDistance(finish, [0, 0])
if distance < minDistance:
minDistance = distance
result = mid
if step % 100 == 0:
print("step {}:\nresult = {}\ndistance = {}".format(step, result, minDistance))
step += 1
return result
print(findSeparatrixPoint2())
fontsize = 20
figure = plt.figure(figsize=(10, 10))
x_min = -5
x_max = 5
y_min = -5
y_max = 5
ob.axes4x4(xmin=x_min - 0.1, xmax=x_max, ymin=y_min - 0.1, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
ax = figure.gca()
start_points = [(5, 1.6250875087508752)]
inits = start_points
ob.phaseportrait(lambda X: np.array([np.sin(X[0]) + np.exp(X[1]) - 1, np.sin(X[0] - X[1])]),
inits, t=(-4.5, 100), n=1000, linewidth=3, color='Teal')
ax.tick_params(axis=u'both', which=u'both',length=0)
plt.legend()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
w = 5
Y, X = np.mgrid[-w:w:1000j, -w:w:1000j]
U = np.sin(X) + np.exp(Y) - 1
V = np.sin(X - Y)
fontsize = 20
x_min = -5
x_max = 5
y_min = -5
y_max = 5
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=x_min, xmax=x_max, ymin=y_min, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
start_points = [(5, 1.6250875087508752)]
inits = start_points
ob.phaseportrait(lambda X: np.array([np.sin(X[0]) + np.exp(X[1]) - 1, np.sin(X[0] - X[1])]),
inits, t=(-4.5, 100), n=1000, linewidth=3, color='Teal')
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.4, 1.4], color='LIGHTCORAL', linewidth=3)
plt.tight_layout()
plt.show()
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
w = 5
Y, X = np.mgrid[-w:w:1000j, -w:w:1000j]
U = np.sin(X) + np.exp(Y) - 1
V = np.sin(X - Y)
fontsize = 20
x_min = -5
x_max = 5
y_min = -5
y_max = 5
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=x_min, xmax=x_max, ymin=y_min, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
inits = [(5, 1.625)]
ob.phaseportrait(lambda X: np.array([np.sin(X[0]) + np.exp(X[1]) - 1, np.sin(X[0] - X[1])]),
inits, t=(-100, 100), n=1000, linewidth=3, color='Teal')
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[1.4, 1.4], color='LIGHTCORAL', linewidth=3)
plt.tight_layout()
plt.show()
def traverse(point):
steps = 10000000
dt = 0.00001
for _ in range(steps):
point[0] += dxdt(point[0], point[1]) * dt
point[1] += dydt(point[0], point[1]) * dt
return point
print("Координаты точки стремления: ({}, {})".format(*traverse([5, 1.624219999])))
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
w = 6
Y, X = np.mgrid[-w:w:1000j, -w:w:1000j]
U = np.sin(X) + np.exp(Y) - 1
V = np.sin(X - Y)
fontsize = 20
x_min = -6
x_max = 5
y_min = -5
y_max = 5
figure = plt.figure(figsize=(10, 10))
ob.axes4x4(xmin=x_min, xmax=x_max, ymin=y_min, ymax=y_max, fontsize=fontsize, labels=('x', 'y'))
start_points = [(5, 1.6251)]
inits = start_points
ob.phaseportrait(lambda X: np.array([np.sin(X[0]) + np.exp(X[1]) - 1, np.sin(X[0] - X[1])]),
inits, t=(-50, 100), n=1000, linewidth=3, color='Teal')
ax = figure.gca()
ax.streamplot(X, Y, U, V, density=[2, 2], color='LIGHTCORAL', linewidth=3)
plt.tight_layout()
plt.show()
print("Координаты точки стремления: ({}, {})".format(*traverse([5, 1.6251])))